//
//  BEWebViewController.m
//  s.
//
//  Created by fanshijian on 2022/9/2.
//  Copyright © 2022 J. All rights reserved.
//

#import "BEWebViewController.h"
#import <NSObject+YYAddForKVO.h>

@interface BEWebViewController () <WKUIDelegate,WKNavigationDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, strong) WKWebView *webView;
@property (weak, nonatomic) UIProgressView *progress;

@property (strong, nonatomic) NSString *url;
@property (strong, nonatomic) NSString *externalTitle; // 外部传过来的值

@property (nonatomic, assign) BOOL firstBack;


@end

@implementation BEWebViewController
- (int)animationType {return 2;}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.view.backgroundColor = KLColor(@"#C8152D");
    
    self.url = self.metaInfo[@"url"];
    self.externalTitle = self.metaInfo[@"title"];
    self.firstBack = [self.metaInfo[@"firstBack"] boolValue];
    
    [self loadUrl];
    self.title = @"正在加载";
    
    [self blackBacker];
}



- (void)loadUrl {
    NSURL *url;
    if ([self.url hasPrefix:@"http"]) {
        url = [NSURL URLWithString:self.url];
    }else if ([self.url hasPrefix:@"file"]) {
        url = [NSURL fileURLWithPath:self.url];
    }
    if (url) {
        [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
    }else {
        [self.webView loadHTMLString:self.url baseURL:nil];
    }
}

- (WKWebView *)webView {
    if (!_webView) {
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        configuration.allowsInlineMediaPlayback = NO;
        
        WKWebView *obj = [[WKWebView alloc] initWithFrame:CGRectMake(0, self.jy_bar_h, kScreenWidth, kScreenHeight - self.jy_bar_h) configuration:configuration];
        [self.view addSubview:_webView = obj];
        obj.navigationDelegate = self;
        obj.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        
        KLWKSelf
        [obj addObserverBlockForKeyPath:@"estimatedProgress" block:^(id  _Nonnull obj, id  _Nonnull oldVal, id  _Nonnull newVal) {
            KLSTSelf
            [self updateProgress:[newVal floatValue]];
        }];
        [obj addObserverBlockForKeyPath:@"title" block:^(id  _Nonnull obj, id  _Nonnull oldVal, id  _Nonnull newVal) {
            KLSTSelf
            self.title = newVal;
        }];
    }
    return _webView;
}

- (UIProgressView *)progress{
    if (!_progress) {
        UIProgressView *obj = [[UIProgressView alloc] init];
        [self.view addSubview:_progress = obj];
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            kMakeTV(self.jy_bar_h);
            kMakeLRV(0);
            kMakeHV(1);
        }];
        obj.trackTintColor = KLColor(@"#EEEEEE");
        obj.progressTintColor = UIColor.systemBlueColor;
    }
    return _progress;
}

#pragma mark - 重写
- (void)setTitle:(NSString *)title {
    if (self.externalTitle) {
//        [super setTitle:self.externalTitle];
//        [super setNavTitle:self.externalTitle];
        self.jy_title = self.externalTitle;
        self.bar.titleColor = @"#FFFFFF";
    }else {
//        [super setTitle:self.title];
        [super setNavTitle:self.title];
    }
}

#pragma mark - 内部方法
- (void)updateProgress:(CGFloat)progress {
    self.progress.progress = progress;
    if (self.progress.progress >= 0.99) {
        dispatch_async(main_queue, ^{
            self.progress.hidden = YES;
        });
    }
}

#pragma mark - WKNavigationDelegate
/** 页面加载失败  */
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error{
    self.title = @"加载失败";
}

@end
